package example;

import java.io.*;
import java.net.*;
import java.rmi.RemoteException;

import message.UniversalMessage;

public class DistributedClient {
	//to run: connect supplies, recipe or budget
	
    public static void main(String[] args) throws Exception {

        Socket kkSocket = null;
        String host = "";
        int port = 8888;
        host = util.lookupHost(args[0]);
        System.out.println(args[0]);
        try {
            kkSocket = new Socket(host, port);//open socket to server           
            ObjectOutputStream out = new ObjectOutputStream(kkSocket.getOutputStream());
            ObjectInputStream in = new ObjectInputStream(kkSocket.getInputStream());
            BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
            String fromUser;
            UniversalMessage fromServer;
    	    while ((fromUser = stdIn.readLine()) != null) {
                    System.out.println("Client: " + fromUser);
                    UniversalMessage queryMsg = new UniversalMessage();
                    queryMsg = buildMessage(fromUser);
                    out.writeObject(queryMsg);
                    String content = "";
    			    if ((fromServer = (UniversalMessage) in.readObject()) != null)  {
    			    	buildResult(fromServer);
//    			    	for(int i=0;i<fromServer.result.size();i++) {
//    			    		Tuples filedValue = new Tuples();
//    		    			filedValue =  fromServer.result.get(i);
//    		    			content += filedValue.tups.get(0)+";";
//    		    		}
//    			    	System.out.println("number of tuples:"+fromServer.result.size());
                    }
    	    }
    	    System.out.println("Client finished input");
    	    out.close();
    	    in.close();
    	    stdIn.close();
    	    kkSocket.close();
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host: "+host);
            System.exit(1);
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for the connection to: " + host);
            System.exit(1);
        }

        
    }

	private static void buildResult(UniversalMessage fromServer) {
		// TODO Auto-generated method stub
		
	}

	private static UniversalMessage buildMessage(String fromUser) {
		// TODO Auto-generated method stub
		UniversalMessage mp = new UniversalMessage();
		
		return null;
	}
  
}
